﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace CarlowPitBoy
{
    static class CurrentMap
    {
        public static string name = "unnamed";
        public static Point coordinate = new Point(-1, -1);
        public static Point gridSnap = new Point(32, 32);
        public static bool underground = false;
        public static bool musicChange = false;
        public static string musicLocation = "";

        public static string terrainSelected  = "";
        public static string itemSelected     = "";
        public static string characerSelected = "";

        public static string[] tersLoaded  { get { return _maxTersLoaded; } }
        public static string[] itemsLoaded { get { return _maxTersLoaded; } }
        public static string[] charsLoaded { get { return _maxTersLoaded; } }

        public static Terrain   currentTerrain   = new Terrain();
        public static Item      currentItem      = new Item();
        public static Character currentCharacter = new Character();

        /// <summary>
        /// State of this form
        /// 0 Minimised (nothing)
        /// 1 for Map Properties
        /// 2 Terrain editor
        /// 3 Item Editor
        /// 4 Character Editor
        /// </summary>
        public static sbyte state = 1;

        private static string[] _maxTersLoaded  = new string[50];
        private static string[] _maxItemsLoaded = new string[50];
        private static string[] _maxCharsLoaded = new string[50];

        private static byte _tersLoaded = 0;
        private static byte _itemsLoaded = 0;
        private static byte _charsLoaded = 0;

        public static void saveAs()
        {
            //if (validateMap()) ;
            //Save all info to an xml
        }

        /// <summary>
        /// Checks all the properties of a map that could make it invalid
        /// </summary>
        /// <returns>Returns true if map is valid, false if invalid</returns>
        private static bool validateMap()
        {
            string warning = "Map not saved";
            if (name == "unnamed")
            {
                MessageBox.Show("Invalid name; cannot be called unnamed", warning);
                return false;
            }
            if (coordinate.X > -1 && coordinate.X <= 5000)
            {
                MessageBox.Show("Invalid X coordinate for map, must be between 0 and 5000; Map not saved", warning);
                return false;
            }
            if (coordinate.Y > -1 && coordinate.Y <= 5000)
            {
                MessageBox.Show("Invalid Y coordinate for map, must be between 0 and 5000; Map not saved", warning);
                return false;
            }
            //All spaces on map filled N.B Future Update
            return true;
        }

        public static void addTerrain(string terrainToAdd)
        {
            if (_tersLoaded >= 50)
                MessageBox.Show("You may not have more than 50 .ter files ", "Terrain Files Loaded Exceeds Limit", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
                _maxTersLoaded[_tersLoaded] = terrainToAdd;
            
        }

        public static void addItem(string itemToAdd)
        {
            if (_itemsLoaded >= 50)
                MessageBox.Show("You may not have more than 50 .item files in any given map.", "Item Files Loaded Exceeds Limit", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
                _maxItemsLoaded[_itemsLoaded] = itemToAdd;          
        }

        public static void addCharacter(string characterToAdd)
        {
            if (_charsLoaded >= 50)
                MessageBox.Show("You may not have more than 50 .char files in any given map.", "Character Files Loaded Exceeds Limit", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
                _maxCharsLoaded[_charsLoaded] = characterToAdd;            
        }

        public static string[] getMemoryConditionChoice1(string memoryCondition)
        {
            //N.B Error Check

            //            ( None )
            //Change Collidable
            //Change Dialogue
            //Change Item Given
            //Create Item at square (Grid Snap applies)
            //Create Character at square (Grid Snap applies)
            //Use {Magic} on {Terrain/Item/Character}
            //Change Exists on Map
            //Use Magic on Object
            //Change Item received
            string[] retStrings;

            if (memoryCondition == "Change Location (Grid Snap applies)" || memoryCondition == "( None )")
                retStrings = new string[] { "N/A" };
            else if (memoryCondition == "Create Item at square (Grid Snap applies)" || memoryCondition == "Change Item received")
            {
                retStrings = new string[_itemsLoaded];
                for (int itemsChecked = 0; itemsChecked < _itemsLoaded; itemsChecked++)
                    retStrings[itemsChecked] = _maxItemsLoaded[itemsChecked];
            }
            else if (memoryCondition == "Create Character at square (Grid Snap applies)")
            {
                retStrings = new string[_charsLoaded];
                for (int charsChecked = 0; charsChecked < _charsLoaded; charsChecked++)
                    retStrings[charsChecked] = _maxCharsLoaded[charsChecked];
            }
            else if (memoryCondition == "Use {Magic} on {Terrain/Item/Character}")
                retStrings = GameProperties.magicTypes;
            else if (memoryCondition == "Change Dialogue")
                retStrings = new string[] { "On Interact", "On Area Enter", "On Item Receive" };
            else if (memoryCondition == "Change Item Given")
            {
                retStrings = new string[_itemsLoaded];
                for (int itemsChecked = 0; itemsChecked < _itemsLoaded; itemsChecked++)
                    retStrings[itemsChecked] = _maxItemsLoaded[itemsChecked];
            }
            else if (memoryCondition == "Change Exists on Map" || memoryCondition == "Change Collidable")
                retStrings = new string[] { "true", "false" };
            else
            {
                MessageBox.Show("Error; Incorrect options in combo box, contact manufacturer. \n Check CurrentMap.cs getMemoryConditionChoice2 method.", "Error,  contact manufacturer", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                retStrings = new string[] { "N/A" };
            }

            return retStrings;
        }

        public static string[] getMemoryConditionChoice2(string memoryCondition)
        {
            //N.B Error Check
            string[] retStrings;

            if (memoryCondition == "Change Location (Grid Snap applies)"
                || memoryCondition == "( None )"
                || memoryCondition == "Create Item at square (Grid Snap applies)"
                || memoryCondition == "Create Character at square (Grid Snap applies)"
                || memoryCondition == "Change Dialogue"
                || memoryCondition == "Change Exists on Map" 
                || memoryCondition == "Change Collidable")
                retStrings = new string[] { "N/A" };
            else if (memoryCondition == "Use {Magic} on {Terrain/Item/Character}")
            {
                int totalLoaded = _tersLoaded + _itemsLoaded + _charsLoaded;
                retStrings = new string[totalLoaded];
                int position = 0;
                int tersChecked = 0;
                int itemsChecked = 0;
                int charsChecked = 0;

                while (tersChecked < _tersLoaded)
                {
                    retStrings[position] = _maxTersLoaded[tersChecked];
                    tersChecked++;
                    position++;
                }
                while (itemsChecked < _itemsLoaded)
                {
                    retStrings[position] = _maxItemsLoaded[itemsChecked];
                    itemsChecked++;
                    position++;
                }
                while (charsChecked < _charsLoaded)
                {
                    retStrings[position] = _maxCharsLoaded[charsChecked];
                    charsChecked++;
                    position++;
                }
            }
            else if (memoryCondition == "Change Dialogue"
                || memoryCondition == "Change Item Given")
                retStrings = new string[] { "True", "False" };
            else
            {
                MessageBox.Show("Error; Incorrect options in combo box, contact manufacturer. \n Check CurrentMap.cs getMemoryConditionChoice2 method.", "Error,  contact manufacturer", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                retStrings = new string[] { "N/A" };
            }

            return retStrings;
        }

        public static string getMemoryConditionChoice3(string memoryCondition)
        {
            string retString;
            if (memoryCondition == "( None )")
                retString = "";
            else if (memoryCondition == "Change Exists on Map"
                || memoryCondition == "Change Collidable"
                || memoryCondition == "Change Item Received"
                || memoryCondition == "Use {Magic} on {Terrain/Item/Character}"
                || memoryCondition == "Change Item Given")
                retString = "N/A";
            else if (memoryCondition == "Change Location (Grid Snap applies)"
                || memoryCondition == "Create Item at square (Grid Snap applies)"
                || memoryCondition == "Create Character at square (Grid Snap applies)")
                retString = "0";
            else if (memoryCondition == "Change Dialogue")
                retString = "Your dialogue here";
            else
            {
                MessageBox.Show("Error; Incorrect options in combo box, contact manufacturer. \n Check CurrentMap.cs getMemoryConditionChoice2 method.", "Error,  contact manufacturer", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                retString = "Error";
            }
            return retString;
        }

        public static string getMemoryConditionChoice4(string memoryCondition)
        {
            string retString;
            if (memoryCondition == "( None )")
                retString = "";
            else if (memoryCondition == "Change Exists on Map"
                || memoryCondition == "Change Collidable"
                || memoryCondition == "Change Item Received"
                || memoryCondition == "Use {Magic} on {Terrain/Item/Character}"
                || memoryCondition == "Change Item Given"
                || memoryCondition == "Change Dialogue")
                retString = "N/A";
            else if (memoryCondition == "Change Location (Grid Snap applies)"
                || memoryCondition == "Create Item at square (Grid Snap applies)"
                || memoryCondition == "Create Character at square (Grid Snap applies)")
                retString = "0";
            else
            {
                MessageBox.Show("Error; Incorrect options in combo box, contact manufacturer. \n Check CurrentMap.cs getMemoryConditionChoice2 method.", "Error,  contact manufacturer", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                retString = "Error";
            }
            return retString;
        }
    }
}
